from django.shortcuts import render
from django.shortcuts import render_to_response
from django.template import RequestContext
#from django.contrib.auth import authenticate, login
#from django.contrib import auth

from django.http import HttpResponseRedirect
from django.core.context_processors import csrf

from login.models import client
from login.models import Hotels
from login.models import Review
from login.models import Room

# must import  loginForm class
from login.models import loginForm
from login.models import searchForm
from login.models import roomForm
from login.models import reviewForm

# import query class
from django.db.models import Q


##############################################################################################################


def index(request):

    #obj_form = loginForm()

    #context = {'obj_form': obj_form}

    return render(request,'login/index.html')




def search_hotel(request):

    hotel_info = Hotels.objects.all()

    # must in a dictionary
    hotel_data ={'hotel_detail':hotel_info}
    # to dispaly data

    #return render_to_response(context_intance=RequestContext(request),'login/search_hotel.html',hotel_data)
    # using render to repaint html page

    return render(request,'login/search_hotel.html',hotel_data)


def gallery(request):

    hotel_id = request.GET['hotel_id']


    # must in a dictionary
    hotel_data_id = {'hotel_id':hotel_id}

    #hotel_id =request.GET[hotel_id]
    #return render(request,'login/gallery.html')

    return render(request,'login/gallery.html',hotel_data_id)




def reviews(request):
    #from from url
    hotel_id = request.GET['hotel_id']

    #hotel_id = 1

    #hotel_info = Hotels.objects.all()
    #user_haslocations = has_location.objects.filter(user_has=user)

    hotel_info = Review.objects.filter(myreview_id_id = hotel_id)

    # must in a dictionary
    hotel_data ={'hotel_detail':hotel_info,'hotel_id':hotel_id}

    # to dispaly data
    # using render to repaint html page

    return render(request,'login/reviews.html',hotel_data)


    #return render(request,'login/reviews.html')


def new_review(request):

    #from from url
    hotel_id = request.GET['hotel_id']

    obj_form = reviewForm()

    context = {'obj_form': obj_form,'hotel_id':hotel_id}

    return render(request,'login/new_review.html',context)




def add_review(request):


    if request.method == 'POST':

        #dont make this###################
        #myreview_user_post = request.POST['myreview_user']
        #myreview_date_post = request.POST['myreview_date']
        #form = {'myreview_user':myreview_user_post}

        form = reviewForm(request.POST)

        if form.is_valid():

            form.save()
            return HttpResponseRedirect('/login/booking')

############# to show form on browser
    args = {}
    args.update(csrf(request))

    args['form'] = reviewForm()
    print args

    return render(request,'/login/new_review.html',args)

##################################################################################################

def booking(request):


        #from from url
        #hotel_id = request.GET['hotel_id']
        global obj_form2

        obj_form2= roomForm()
        obj_form = searchForm()
        context = {'obj_form': obj_form,'obj_form2':obj_form2}


        #send2={'obj_form2':obj_form2}

        #context = {'obj_form': obj_form,'hotel_id':hotel_id}
        #return render(request,'login/booking.html',context)

        return render(request,'login/booking.html',context)


def check_ava(request):

    if request.method == 'POST':

        hotel_name_post = request.POST['hotel_name']
        country_post = request.POST['country']
        busy_start_post = request.POST['busy_start']
        busy_end_post = request.POST['busy_end']
        room_type_post = request.POST['room_type']



# note keda get >>>>return one object record
#           filter >>>> return object has many records

        hotel_info = Hotels.objects.get(

            Q(hotel_name=hotel_name_post),
            Q(country=country_post),
            )
        # must in a dictionary
        #hotel_data ={'hotel_info':hotel_info}

        if not hotel_info:
            return render(request,'login/no_result.html')


        else:
            room_info =Room.objects.filter(
                #forign key
                Q(hotel_id_id=hotel_info.id),

                Q(room_type=room_type_post),

                ~Q(busy_start=busy_start_post) & ~Q(busy_end=busy_end_post) &

                ~Q(busy_start__range=(busy_start_post,busy_end_post)) &

                ~Q(busy_end__range=(busy_start_post,busy_end_post))
            )[:1]


            if not room_info:
                return render(request,'login/no_result.html')

            else:

                # must in a dictionary
                room_data ={'room_info':room_info,'hotel_info':hotel_info,

                            'busy_start':busy_start_post,'busy_end':busy_end_post}

                return render(request,'login/list_rooms.html',room_data)

#############################################################################################################
def make_reservation(request):

    #form=Room()

    if request.method == 'POST':




        hotel_id_post = request.POST['hotel_id']
        room_type_post = request.POST['room_type']
        room_desc_post = request.POST['room_desc']
        room_price_post = request.POST['room_price']
        busy_start_post = request.POST['busy_start']
        busy_end_post = request.POST['busy_end']

            # to insert into DB Room(is name of table & there fields)

            #b = Blog(name='Beatles Blog', tagline='All the latest Beatles news.')
            #b.save()

        obj_form2 = Room(hotel_id_id=hotel_id_post,
                room_type=room_type_post,
                room_desc=room_desc_post,
                room_price=room_price_post,
                busy_start=busy_start_post,
                busy_end=busy_end_post
                )





        obj_form2.save()

        return render(request,'login/make_reservation.html')



#############################################################################################################
def no_result(request):

    return render(request,'login/no_result.html')


###################################################################################################################


'''
        Poll.objects.get(
            Q(question__startswith='Who'),
            Q(pub_date=date(2005, 5, 2)) | Q(pub_date=date(2005, 5, 6))
        )
        SELECT * from Hotels
        WHERE hotel_name LIKE '' AND country LIKE '' AND room_type LIKE ''
        AND (busy_start != '2005-05-02' AND busy_end != '2005-05-06')


        Entry.objects.filter(pub_date__range=(start_date, end_date))

        dy method range
        ******************
                __range



        hotel_info = Hotels.objects.filter(

            Q(hotel_name=hotel_name_post),
            Q(country=country_post),
            Q(room_type=room_type_post),

            ~Q(busy_start=busy_start_post) & ~Q(busy_end=busy_end_post) &

            ~Q(busy_start__range=(busy_start_post,busy_end_post)) &

            ~Q(busy_end__range=(busy_start_post,busy_end_post))

        )

        # must in a dictionary
        hotel_data ={'hotel_info':hotel_info}

        if not hotel_info:
            return render(request,'login/no_result.html')
            #HttpResponseRedirect('/login/no_result')

        else:
            return render(request,'login/list_rooms.html',hotel_data)
'''